package TP4;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.cybergarage.x3d.SceneGraph;


import VAMP.*;

public class TerraGenVRML extends Thread implements vAlgo {
	private VAMP v;

	public TerraGenVRML(VAMP v){
		this.v=v;
		super.start();
	}

	public synchronized void run(){
		while(true){
			v.AtTheBegining(this);
			vGrey[][] img= (vGrey[][]) v.LoadImage(VAMP.tImage.GreyScale).data;
			int width=img.length;
			int height=img[0].length;
			String name= v.ReadValueString("nom du fichier qui contiendra la map VRML (sans l'extension)");
			File f = new File(name+".wrl");
			BufferedWriter bw;
			try {
				bw = new BufferedWriter(new FileWriter(f));

			bw.write("#VRML V2.0 utf8\n" +
					"Transform{\n" +
					"    scale 1 0.1 1\n" +
					"    translation	-"+width/2+" 0 -"+height/2+"\n" +
					"    children[\n" +
					"			Shape{\n" +
					"				appearance Appearance {\n"+
					"					material Material {\n"+
					"						specularColor 0.71 0.70 0.56\n"+
					"						shininess 0.16\n"+
					"						ambientIntensity 0.4\n"+
					"					}\n"+
					"			}\n"+
					"			geometry ElevationGrid {\n" +
					"				creaseAngle 2\n" +
					"				xDimension "+width+"\n" +
					"				zDimension "+height+"\n" +
					"				xSpacing 1.0\n" +
					"				zSpacing 1.0\n" +
					"				height [\n					");

			for(int i=0; i<height; i++){
				for(int j=0; j<width; j++){
					int h=(img[j][i].getColorValues()[0]/10)*10;
					bw.write(255-h+" ,");
				}
				bw.write("\n					");
			}
			bw.write("		]\n");
			bw.write("		}\n" +
					"		}" +
					"	]\n" +
			"}\n");
			bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			SceneGraph sg = new SceneGraph(SceneGraph.NORMAL_GENERATION);
			new ViewerJ3D(sg);
	}

}

}
